
var chatData = {
    chatId:0,
    userId:0,
    wantNum:10,
    isLoading : false,
    contextPath : null
}

//初始化数据
function initChatData(userId,chatId,contextPath){
    window.console.log("initChatData...");
    chatData.userId = userId;
    chatData.chatId = chatId;
    chatData.contextPath = contextPath;
}

function sendMsg(){
    //首先在聊天窗口显示刚刚发送的消息
    var text = $("#msgInput").val();
    $("#chatBox").append(text+"<br/>");
    //调用接口发送消息存入数据库
    sendToServer(text);
    $("#msgInput").val('');
}

//把消息通过接口方式发送给服务器存储
//并非直接通过ws直接发送消息
function sendToServer(text){
    var url = "/message/sendMsg.json";
    $.post(url,{chatId:chatData.chatId,content:text}, function (data) {
        if(data.ret>0){
            window.console.log("send message success ...");
            //发送成功之后马上去数据库请求新的数据
            getNewMsg();
        }
    });
}

/**
 * 向数据库获取新的消息
 */
function getNewMsg(){
    if(!chatData.isLoading) {
        //枷锁
        chatData.isLoading = true;
        var url = "/message/getNewMsg.json";
        $.post(url, {chatId: chatData.chatId}, function (data) {
            if (data.ret > 0) {
                $("#chatBox").html('');
                window.console.log("get new msg success ...");
                //将获取的新消息显示在聊天窗口
                var msgs = data.object.messageList;
                $.each(msgs, function (k, v) {
                    var userId = v.userId;
                    var userName = v.userName;
                    var content = v.content;
                    var createTime = v.createTime;
                    var date = new Date(createTime);
                    var strdate = formatDate(date);
                    //console.log(userId+","+userName+","+content+","+createTime);
                    if (userId == chatData.userId) {
                        $("#chatBox").append("<li style='float: right;clear: both'><p>" + strdate + "</p><p>[" + userName + "]:" + content + "</p></li>");
                    } else {
                        $("#chatBox").append("<li style='clear: both'><p>" + strdate + "</p><p>[" + userName + "]:" + content + "</p></li>");
                    }
                });
                chatData.isLoading = false;
            }
        });
    }
}

/**
 * 加载更多消息
 */
function loadMore(){

}

function WsOnOpenProcess(m){
    if (window.console)
        window.console.log("WsOnOpenProcess-->begin to send chatid to server: "+chatData.chatId);
    ChatWS.send_message("LIVECONNECT " + chatData.chatId);
}

function WsOnMessageProcess(m){
    var obj = $.parseJSON(m);
    var command = obj.command;
    var data = $.parseJSON(obj.data);
    console.log("ws command ----->" + command);
    console.log("ws message ----->" + data);
    $("#logBox").append("<li>[ws-->command]:"+command+"</li>")
    $("#logBox").append("<li>[ws-->message]:"+data.content+"</li>")
    if(command=='CHATREFLASH'){
        getNewMsg();
    }
}

$(document).ready(function () {
    getNewMsg();
});

//ctrl+enter
$(document).keydown(function (event) {
    if (event.ctrlKey && event.keyCode == 13) {
        sendMsg();
    }
});


function  formatDate(now)   {
    var   year=now.getFullYear();
    var   month=now.getMonth()+1;
    var   date=now.getDate();
    var   hour=now.getHours();
    var   minute=now.getMinutes();
    var   second=now.getSeconds();
    return   year+"-"+month+"-"+date+"   "+hour+":"+minute+":"+second;
}